package com.jiazhong.schedule.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;


public class BaseController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取路径，进行路径的修改
        String requestURI = req.getRequestURI();
        String[] split = requestURI.split("/");
        String string = split[split.length - 1];
        System.out.println(string);
        //通过反射执行要执行的方法
        Class clazz = this.getClass();
        try {
            Method declaredMethod = clazz.getDeclaredMethod(string, HttpServletRequest.class, HttpServletResponse.class);
            //设置方法可以进行访问
            declaredMethod.setAccessible(true);
            //调用对应的方法
            declaredMethod.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
